package com.eboss2.sbs.weight;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eboss2.sbs.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnLongClick;

/**
 * Created by Administrator on 2018/3/20 0020.
 */

public class NumberKeyboardDialog extends LinearLayout {

    @BindView(R.id.number_1)
    TextView number1;
    @BindView(R.id.number_2)
    TextView number2;
    @BindView(R.id.number_3)
    TextView number3;
    @BindView(R.id.number_4)
    TextView number4;
    @BindView(R.id.number_5)
    TextView number5;
    @BindView(R.id.number_6)
    TextView number6;
    @BindView(R.id.number_7)
    TextView number7;
    @BindView(R.id.number_8)
    TextView number8;
    @BindView(R.id.number_9)
    TextView number9;
    @BindView(R.id.number_clear_last)
    ImageView numberClearLast;
    @BindView(R.id.number_0)
    TextView number0;
    @BindView(R.id.number_enter)
    TextView numberEnter;
    @BindView(R.id.scan)
    TextView scan;

    private NumberClickListener numberClickListener;
//    private AlertDialog numberDialog;

    private PopupWindow mPop;
    private View popView;


    public NumberKeyboardDialog(Context context) {
        super(context);

    }

    public NumberKeyboardDialog(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NumberKeyboardDialog(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void show(Context context, View view) {
        if (mPop == null) {
            popView = LayoutInflater.from(context).inflate(R.layout.number_keyboard, this, false);
            ButterKnife.bind(this, popView);
            mPop = new PopupWindow();
            mPop.setContentView(popView);
            mPop.setTouchable(true);
            mPop.setFocusable(false);
            mPop.setOutsideTouchable(false);
            mPop.setBackgroundDrawable(new ColorDrawable());
            mPop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            mPop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            mPop.setAnimationStyle(R.style.PopWindowstyle);

        }
        mPop.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    @OnClick({R.id.number_1, R.id.number_2, R.id.number_3, R.id.number_4, R.id.number_5, R.id.number_6, R.id.number_7, R.id.number_8, R.id.number_9, R.id.number_0, R.id.number_clear_last, R.id.number_enter, R.id.scan})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.number_1:
                numberClickListener.clickNumber(number1.getText().toString());
                break;
            case R.id.number_2:
                numberClickListener.clickNumber(number2.getText().toString());
                break;
            case R.id.number_3:
                numberClickListener.clickNumber(number3.getText().toString());
                break;
            case R.id.number_4:
                numberClickListener.clickNumber(number4.getText().toString());
                break;
            case R.id.number_5:
                numberClickListener.clickNumber(number5.getText().toString());
                break;
            case R.id.number_6:
                numberClickListener.clickNumber(number6.getText().toString());
                break;
            case R.id.number_7:
                numberClickListener.clickNumber(number7.getText().toString());
                break;
            case R.id.number_8:
                numberClickListener.clickNumber(number8.getText().toString());
                break;
            case R.id.number_9:
                numberClickListener.clickNumber(number9.getText().toString());
                break;
            case R.id.number_0:
                numberClickListener.clickNumber(number0.getText().toString());
                break;
            case R.id.number_clear_last:
                numberClickListener.clickClearLast();
                break;
            case R.id.scan:
                numberClickListener.clickScan(mPop);
                break;
            case R.id.number_enter:
                numberClickListener.clickEnter(mPop);
                break;
        }
    }

    @OnLongClick({R.id.number_clear_last})
    public boolean onLongClick(View view) {
        switch (view.getId()) {
            case R.id.number_clear_last:
                numberClickListener.longClickClearLast();
                break;
        }
        return false;
    }


    public void setNumberClickListener(NumberClickListener numberClickListener) {
        this.numberClickListener = numberClickListener;
    }

    public interface NumberClickListener {
        void clickNumber(String number);

        void clickNumber0(String number0);

        void clickClearLast();

        void longClickClearLast();

        void clickEnter(PopupWindow mPop);

        void clickScan(PopupWindow mPop);
    }
}